# -*- coding: UTF-8 -*-
# 官网：  https://github.com/giampaolo/psutil

import psutil
import time

def get_cpu():
    for x in range(3):
        time.sleep(1)
        cpu_liyonglv = psutil.cpu_percent(interval=1)
        print("当前cpu利用率：\033[1;31;42m%s%%\033[0m"%cpu_liyonglv)


def memissue(): 
    """ ['内存total:16384.0MB', 'used:9192.3MB', 'baifen:56.5%'] """   
    mem= psutil.virtual_memory()
    #单位换算为MB
    memtotal= mem.total/1024/1024
    memused = mem.used/1024/1024
    membaifen = mem.percent
    return ["内存total:%.1fMB"% memtotal, "used:%.1fMB"% memused, "baifen:%.1f%%"%(membaifen)]


def diskInfo():
    """
    ['硬盘总容量465.72GB', '使用404.34GB', '剩余61.38GB', '已使用86.82%']
    """
    d = psutil.disk_usage('/')
    total = round(d.total /1024/1024/1024, 2)
    free = round(d.free/1024/1024/1024, 2)
    percent = round((total - free) * 100/total, 2)
    return ['硬盘总容量%sGB'%total, '使用%.2fGB'%(total - free), '剩余%sGB'%free, '已使用%s%%'%percent]



if __name__ == '__main__':
    get_cpu()
    print('*'*20)
    print(memissue())
    print('*'*20)
    print(diskInfo())
    

